package com.shape;

import java.util.ArrayList;
import java.util.List;

public class ShapeCalculator {
    private List<Shape> shapes;

    public ShapeCalculator() {
        shapes = new ArrayList<>();
    }

    public void addShape(Shape shape) {
        shapes.add(shape);
        System.out.println("添加成功: " + shape.getName());
    }

    public void displayAllShapes() {
        System.out.println("\n=== 所有图形信息 ===");
        if (shapes.isEmpty()) {
            System.out.println("暂无图形数据");
            return;
        }

        for (int i = 0; i < shapes.size(); i++) {
            System.out.println((i + 1) + ". " + shapes.get(i));
        }
    }

    public void displayStatistics() {
        if (shapes.isEmpty()) {
            System.out.println("暂无图形数据");
            return;
        }

        double totalArea = 0;
        double totalPerimeter = 0;

        for (Shape shape : shapes) {
            totalArea += shape.getArea();
            totalPerimeter += shape.getPerimeter();
        }

        System.out.println("\n=== 统计信息 ===");
        System.out.printf("图形总数: %d\n", shapes.size());
        System.out.printf("总面积: %.2f\n", totalArea);
        System.out.printf("总周长: %.2f\n", totalPerimeter);
        System.out.printf("平均面积: %.2f\n", totalArea / shapes.size());
        System.out.printf("平均周长: %.2f\n", totalPerimeter / shapes.size());
    }

    public void displayByType() {
        if (shapes.isEmpty()) {
            System.out.println("暂无图形数据");
            return;
        }

        int circleCount = 0, rectangleCount = 0, triangleCount = 0, squareCount = 0;

        for (Shape shape : shapes) {
            if (shape instanceof Circle) circleCount++;
            else if (shape instanceof Rectangle) rectangleCount++;
            else if (shape instanceof Triangle) triangleCount++;
            else if (shape instanceof Square) squareCount++;
        }

        System.out.println("\n=== 按类型统计 ===");
        System.out.println("圆形: " + circleCount + "个");
        System.out.println("矩形: " + rectangleCount + "个");
        System.out.println("三角形: " + triangleCount + "个");
        System.out.println("正方形: " + squareCount + "个");
    }

    public int getShapeCount() {
        return shapes.size();
    }
}